package com.toolbox.dal.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.toolbox.bo.AccountBO;
import com.toolbox.dal.repository.InfraRoleMenuDORepository;
import com.toolbox.dal.mapper.InfraRoleMenuMapper;
import com.toolbox.dal.model.InfraRoleMenuDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色菜单表 服务实现类
 * </p>
 *
 * @author Mango
 * @since 2023-12-01
 */
@SuppressWarnings("ALL")
@Slf4j
@Service
public class InfraRoleMenuDORepositoryImpl extends ServiceImpl<InfraRoleMenuMapper, InfraRoleMenuDO> implements InfraRoleMenuDORepository {

    @Autowired
    private InfraRoleMenuMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void save(Long roleId, List<Long> menuIds, AccountBO bo) {
        List<InfraRoleMenuDO> list = Lists.newArrayList();

        for (Long menuId : menuIds) {
            InfraRoleMenuDO infraRoleMenuDO = new InfraRoleMenuDO();

            infraRoleMenuDO.setRoleId(roleId);
            infraRoleMenuDO.setMenuId(menuId);

            infraRoleMenuDO.setCreatorId(bo.getUserId());
            infraRoleMenuDO.setCreatorName(bo.getNickname());

            infraRoleMenuDO.setModifierId(bo.getUserId());
            infraRoleMenuDO.setModifierName(bo.getNickname());
        }

        // 批量添加
        saveBatch(list,list.size());
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void save(Long roleId, Long menuId, AccountBO bo) {
        InfraRoleMenuDO infraRoleMenuDO = new InfraRoleMenuDO();

        infraRoleMenuDO.setRoleId(roleId);
        infraRoleMenuDO.setMenuId(menuId);

        infraRoleMenuDO.setCreatorId(bo.getUserId());
        infraRoleMenuDO.setCreatorName(bo.getNickname());

        infraRoleMenuDO.setModifierId(bo.getUserId());
        infraRoleMenuDO.setModifierName(bo.getNickname());

        mapper.insert(infraRoleMenuDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void removeByRoleId(Long roleId) {
        mapper.delete(new QueryWrapper<InfraRoleMenuDO>().lambda().eq(InfraRoleMenuDO::getRoleId,roleId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void removeByMenuId(Long menuId) {
        mapper.delete(new QueryWrapper<InfraRoleMenuDO>().lambda().eq(InfraRoleMenuDO::getMenuId,menuId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void removeById(Long id) {
        mapper.deleteById(id);
    }

}